#!/bin/sh
# Lanceur macOS de l'Atelier Mise en Page (Contents/MacOS de « Atelier Mise en Page.app »).
# Il installe la page de l'application dans ~/Library/Application Support/AtelierMiseEnPage et l'ouvre dans une
# fenêtre d'application Chrome, Edge, Brave ou Chromium (sinon dans le navigateur par défaut).
# Avec --maj, il recherche et installe une mise à jour : le bouton « Mise à jour » de l'application ouvre le lien
# atelier-maj:verifier, que macOS confie à la petite application Contents/Helpers/AtelierMAJ.app, qui nous appelle.
ICI=$(cd "$(dirname "$0")" && pwd)
APP=$(cd "$ICI/../.." && pwd)
RES="$APP/Contents/Resources"
. "$RES/config.sh"
ICI_COMMUN=$RES
DONNEES=${ATELIER_DONNEES:-"$HOME/Library/Application Support/AtelierMiseEnPage"}
PAGE="$DONNEES/app/index.html"
PAQUET=mac
. "$RES/commun.sh"
TITRE="Atelier Mise en Page"

message() { # $1 : texte ; $2 : note, caution ou stop
  if silencieux; then journal "$1"; return; fi
  osascript -e 'on run argv' -e 'activate' \
    -e "display dialog (item 1 of argv) with title \"$TITRE\" buttons {\"OK\"} default button \"OK\" with icon ${2:-note}" \
    -e 'end run' "$1" >/dev/null 2>&1
}

question() { # $1 : texte, $2 : bouton qui accepte, $3 : bouton qui refuse ; vrai si on accepte
  if silencieux; then journal "$1"; return 0; fi
  osascript -e 'on run argv' -e 'activate' \
    -e 'display dialog (item 1 of argv) with title (item 4 of argv) buttons {item 3 of argv, item 2 of argv} default button (item 2 of argv) cancel button (item 3 of argv) with icon note' \
    -e 'end run' "$1" "$2" "$3" "$TITRE" >/dev/null 2>&1
}

# Fait connaître à macOS l'assistant de mise à jour (lien atelier-maj:), à l'endroit où se trouve l'application.
enregistrer_maj() {
  aide="$APP/Contents/Helpers/AtelierMAJ.app"
  [ -d "$aide" ] || return
  # L'application vient d'être ouverte par vous : l'assistant qu'elle contient n'a pas à être approuvé une 2e fois.
  xattr -dr com.apple.quarantine "$aide" 2>/dev/null
  lsr=/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister
  [ -x "$lsr" ] && "$lsr" -f "$aide" >/dev/null 2>&1
}

lancer() {
  installer_page_fournie "$RES/index.html" || { message "$(txt demarrage "$DONNEES")" stop; exit 1; }
  adresse="file://$(encoder_url "$PAGE")"
  if silencieux; then journal "Fenêtre : $adresse"; return; fi
  enregistrer_maj &
  for nom in "Google Chrome" "Microsoft Edge" "Brave Browser" "Chromium"; do
    for dossier in /Applications "$HOME/Applications"; do
      if [ -d "$dossier/$nom.app" ]; then
        open -na "$dossier/$nom.app" --args --app="$adresse" --user-data-dir="$DONNEES/profil" \
          --no-first-run --no-default-browser-check --window-size=1440,900 && exit 0
      fi
    done
  done
  # Pas de navigateur de la famille Chrome : la page s'ouvre dans le navigateur par défaut (Safari...)
  open "$PAGE"
}

mettre_a_jour() {
  if ! maj_verifier; then message "$MAJ_MESSAGE" caution; maj_nettoyer; return; fi
  case $MAJ_ETAT in
    a-jour) message "$(txt a-jour "$MAJ_ACTUELLE")" note ;;
    paquet)
      if [ -n "$MAJ_LIEN" ] && question "$(txt paquet "$MAJ_VERSION" "$MAJ_ACTUELLE" Mac)" "$(txt b-telecharger)" "$(txt b-plus-tard)"; then
        silencieux || open "$MAJ_LIEN"
      fi ;;
    disponible)
      if question "$(texte_question)" "$(txt b-installer)" "$(txt b-plus-tard)"; then
        if maj_installer; then
          message "$(txt installee "$MAJ_VERSION")" note
          maj_nettoyer
          lancer
        else
          message "$MAJ_MESSAGE" caution
        fi
      fi ;;
  esac
  maj_nettoyer
}

case "${1:-}" in
  --maj|atelier-maj:*) mettre_a_jour ;;
  *) lancer ;;
esac
